//
//  RJHomeTopView.m
//  AirQuality
//
//  Created by Po on 15/3/11.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJHomeTopView.h"
#import "UIImage+RJImage.h"
#import "RJHomeTopMap.h"
#import "RJLoadView.h"
#import "RJHomeTopSectionView.h"

@interface RJHomeTopView () <RJHomeTopSectionViewDelegate,
                            RJHomeTopMapDelegate>
@property (retain, nonatomic) UIView * segmentBgView;
@property (retain, nonatomic) RJHomeTopSectionView * segmentView;          //分段控制器
@property (retain, nonatomic) UIImageView * segmentSelectedBg;               //分段选中背景
@property (retain, nonatomic) RJHomeTopMap * mapView;                        //网页
@property (retain, nonatomic) UIImageView * mapAqRankImage;                  //污染等级示意图

- (void)initData;
- (void)initInterface;
@end

@implementation RJHomeTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self initInterface];
        
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"netRequestFail" object:nil];
}

#pragma mark - user-defined initialization function

- (void)initData
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequestFail) name:@"netRequestFail" object:nil];
}

- (void)initInterface
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,35, ScreenWidth, self.bounds.size.height)];
    [self addSubview:view];
    [self addSubview:[self getSegmentedControl]];
    [view addSubview:[self getMapView]];
    [view addSubview:[self getMapAqRankImage]];
    view.clipsToBounds = YES;
    
}

- (RJHomeTopSectionView *)getSegmentedControl
{
    if (!_segmentView) {
        NSArray * array = @[@"AQI",@"SO₂",@"NO₂",@"CO",@"O₃",@"PM₁₀",@"PM"];
        _segmentView = [[RJHomeTopSectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 35) titles:array];
        [_segmentView setSelectCount:0];
        [_segmentView setDelegate:self];
    }
    return _segmentView;
}

- (RJHomeTopMap *)getMapView
{
    if (!_mapView) {
        _mapView = [[RJHomeTopMap alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.bounds.size.height - 35)];
        _mapView.delegate = self;
    }
    return _mapView;
}

- (UIImageView *)getMapAqRankImage
{
    if (!_mapAqRankImage) {
        _mapAqRankImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/5, self.frame.size.height - 75, self.frame.size.width /5*4, 40)];
        [_mapAqRankImage setImage:[UIImage imageNamed:@"mapAQIRank"]];
    }
    return _mapAqRankImage;
}

#pragma mark - Notifaction
- (void)netRequestFail
{
    if (_delegate) {
        [_delegate netRequestFailure];
    }
}

#pragma mark - RJHomeTopSectionViewDelegate
- (BOOL)HomeTopSectionView:(RJHomeTopSectionView *)sectionView didSelectCount:(NSUInteger)count isLeft:(BOOL)isLeft
{
//    NSArray * typeArray = @[@"AQI",@"PM2_5",@"PM10",@"O3",@"SO2",@"NO2",@"CO"];
    NSArray * typeArray = @[@"AQI",@"SO2",@"NO2",@"CO",@"O3",@"PM10",@"PM2_5"];
    NSString * type = typeArray[count];
    [_mapView updateWithButtonString:type];
    [self changeRankImageWithType:type];
    return YES;
}

#pragma mark - function

- (void)netRetry
{
    [_mapView getRealAQINet];
}

- (void)changeRankImageWithType:(NSString *)type
{
    if (!_mapAqRankImage) {
        [self getMapAqRankImage];
    }
    [_mapAqRankImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"map%@Rank",type]]];
}

- (void)reloadView
{
    [_segmentView setSelectCount:0];
    [_mapView reloadView:[RJUserData defaultRJUserData].isProvince cityName:[RJUserData defaultRJUserData].currentCity cityCode:[RJUserData defaultRJUserData].currentCityCode];
}

- (void)mapViewDisappear
{
    [_mapView mapViewDisappear];
}
- (void)touchPaoPao:(NSString *)cityNamel
{
    if (_delegate) {
        [_delegate touchPaoPaoView:cityNamel];
    }
}
@end
